package org.middleware.utils;

import java.awt.*;
import java.awt.image.BufferedImage;
import java.util.Random;

public class CaptchaUtil {

    // 验证码字符集（去掉容易混淆的字符）
    private static final String CHAR_SET = "23456789ABCDEFGHJKLMNPQRSTUVWXYZ";
    private static final int WIDTH = 120;
    private static final int HEIGHT = 40;
    private static final int CODE_LENGTH = 4;

    /**
     * 生成验证码图片和文本
     */
    public static CaptchaResult generateCaptcha() {
        BufferedImage image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);
        Graphics2D g = image.createGraphics();

        // 设置背景色
        g.setColor(Color.WHITE);
        g.fillRect(0, 0, WIDTH, HEIGHT);

        // 生成随机验证码文本
        String captchaText = generateRandomText();

        // 绘制验证码文本
        drawText(g, captchaText);

        // 添加干扰线
        drawInterferenceLines(g);

        // 添加噪点
        drawNoise(image);

        g.dispose();

        return new CaptchaResult(captchaText, image);
    }

    private static String generateRandomText() {
        Random random = new Random();
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < CODE_LENGTH; i++) {
            sb.append(CHAR_SET.charAt(random.nextInt(CHAR_SET.length())));
        }
        return sb.toString();
    }

    private static void drawText(Graphics2D g, String text) {
        g.setFont(new Font("Arial", Font.BOLD, 24));
        Random random = new Random();

        for (int i = 0; i < text.length(); i++) {
            // 随机颜色
            g.setColor(new Color(
                    random.nextInt(150),
                    random.nextInt(150),
                    random.nextInt(150)
            ));

            // 随机位置和旋转
            int x = 20 + i * 25;
            int y = 25 + random.nextInt(10);
            double rotate = random.nextDouble() * 0.3 - 0.15; // -15° 到 +15°

            g.rotate(rotate, x, y);
            g.drawString(String.valueOf(text.charAt(i)), x, y);
            g.rotate(-rotate, x, y);
        }
    }

    private static void drawInterferenceLines(Graphics2D g) {
        Random random = new Random();
        g.setStroke(new BasicStroke(1.5f));

        for (int i = 0; i < 5; i++) {
            g.setColor(new Color(
                    random.nextInt(200),
                    random.nextInt(200),
                    random.nextInt(200)
            ));
            int x1 = random.nextInt(WIDTH);
            int y1 = random.nextInt(HEIGHT);
            int x2 = random.nextInt(WIDTH);
            int y2 = random.nextInt(HEIGHT);
            g.drawLine(x1, y1, x2, y2);
        }
    }

    private static void drawNoise(BufferedImage image) {
        Random random = new Random();
        for (int i = 0; i < 100; i++) {
            int x = random.nextInt(WIDTH);
            int y = random.nextInt(HEIGHT);
            image.setRGB(x, y, random.nextInt(0xFFFFFF));
        }
    }

    /**
     * 验证码结果封装类
     */
    public static class CaptchaResult {
        private final String text;
        private final BufferedImage image;

        public CaptchaResult(String text, BufferedImage image) {
            this.text = text;
            this.image = image;
        }

        public String getText() { return text; }
        public BufferedImage getImage() { return image; }
    }
}
